﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class OperationController : MonoBehaviour
{
    public static OperationController Instance;
    private void Awake()
    {
        Instance = this;
    }
    public int frameIndex;//帧编号
    private void Start()
    {
        if (GameController.Instance.applicationType == SceneEnum.Client)
        {
            operationInfo = new OperationInfo();
            Info info = new Info();
            info.infoType = 1;
            Client.Instance.OnClickToSendServer(LitJson.JsonMapper.ToJson(info));
        }
    }
    #region client
    public OperationInfo operationInfo;//该客户端的操作信息
    public List<Role> roles = new List<Role>();//角色列表

    public void GetRoles()
    {
        Transform playerParent = GameObject.Find("PlayerParent").transform;
        foreach (Transform item in playerParent)
        {
            if (item.GetComponent<Role>()) roles.Add(item.GetComponent<Role>());
        }
    }
    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.W)) operationInfo.isClickW = true;
        else if (Input.GetKeyUp(KeyCode.W)) operationInfo.isClickW = false;
        if (Input.GetKeyDown(KeyCode.S)) operationInfo.isClickS = true;
        else if (Input.GetKeyUp(KeyCode.S)) operationInfo.isClickS = false;
        if (Input.GetKeyDown(KeyCode.A)) operationInfo.isClickA = true;
        else if (Input.GetKeyUp(KeyCode.A)) operationInfo.isClickA = false;
        if (Input.GetKeyDown(KeyCode.D)) operationInfo.isClickD = true;
        else if (Input.GetKeyUp(KeyCode.D)) operationInfo.isClickD = false;
    }
    /// <summary>
    /// 拿到服务器发送的所有单位的操作 将操作下发各操作单位
    /// </summary>
    /// <param name="operation"></param>
    public void ServerOperationSet(Operation operation)
    {
        if (roles.Count == 0) GetRoles();
        if (operation != null && operation.operationInfos != null)
        {
            if (frameIndex == operation.frameIndex - 1)
            {
                for (int i = 0; i < operation.operationInfos.Count; i++)
                {
                    if (roles[i].transform.name == operation.index.ToString())
                    {
                        roles[i].SetOperation(operation.operationInfos[i]);
                    }
                }
            }
        }
        
        frameIndex = operation.frameIndex;
    }
    #endregion
    #region server
    /// <summary>
    /// 操作备份
    /// int  帧编号
    /// <int, OperationInfo> 单位编号  单位操作
    /// </summary>
    public Dictionary<int, Dictionary<int, OperationInfo>> operationBackups = new Dictionary<int, Dictionary<int, OperationInfo>>();
    /// <summary>
    /// 收到客户端上传操作
    /// </summary>
    /// <param name="operation"></param>
    public void ClientOperationSet(Operation operation)
    {
        bool isInclude = false;
        int index = 0;
        if (operationBackups.ContainsKey(operation.frameIndex))//存在该帧编号
        {
            //遍历是否存有 该帧  该单位 的操作
            foreach (var item in operationBackups[operation.frameIndex])
            {
                if(item.Key == operation.index)
                {
                    isInclude = true;
                    index = item.Key;
                }
            }
            if (isInclude)
            {
                operationBackups[operation.frameIndex][index] = operation.operationInfo;
            }
            else
            {
                operationBackups[operation.frameIndex].Add(operation.index,operation.operationInfo);
            }
        }
        else
        {
            Dictionary<int, OperationInfo> keyValuePairs = new Dictionary<int, OperationInfo>();
            keyValuePairs.Add(operation.index,operation.operationInfo);
            operationBackups.Add(operation.frameIndex, keyValuePairs);
        }
    }
    #endregion


}
